/**
 * Created by zhangpan on 2017/5/17.
 */
$(function () {
    var $box       = $('.is-friend .box'),
        $boxVis    = $('.is-friend .box, .is-master .box'),
        $other     = $('.is-friend .host, .is-friend .word'),
        $tpBg      = $('.tp-bg'),
        $tpBgDesk  = $('.tp-bg em:first'),
        $tpBgChair = $('.tp-bg em:last'),
        $oldPeople = $('.word em:first'),
        $nowPeople = $('.word em:last');

    //查看排行榜
    $('#show-ranking').on('click',function () {
        $navIcon.fadeIn();
        $('.ranking-box,.desk-box').css('transform','translateY(-100%)');
    });

    //添加座椅
    $add.on('click',function () {
        if($('.wrapper-big .circle,.zoom-in .circle').length==$('.has-man').length){
            $add.before($wrapperC.clone());
            $('.is-friend .zoom-in em:last').html($('.is-friend .zoom-in').length);
        }else{
            showPop('#dissatisfy-pop');
        }
    });

    //载入已入席宾客
    $.ajax({
        type: "GET", 				//请求的方式
        url: "../js/visitor.json",  //请求的路径地址
        async: true, 				//是否异步
        success: function(res) { 	//请求成功并返回数据的回调
            showVisitor(res);
            $oldPeople.html(res.length+1);
            $nowPeople.html(res.length);
        },
        error: function(a, b, c) {
            console.log(a); //错误对象
            console.log(b); //错误类型
            console.log(c); //错误的具体信息
        },
    });
    function showVisitor(res) {
        $.each(res,function () {
            $boxVis.children().eq(this.desk).find('.circle:eq('+this.chair+')')
                .addClass('has-man').html('<img src="'+this.img+'">');
        });
        // $('.wrapper-big .circle,.zoom-in .circle').addClass('has-man')
        //     .html('<img src="../img/touxiang1.png">');
    }

    //点击放大选择席位
    var $nowZoom,nowZoomFlag=true;
    $box.on('click','.wrapper-big',function () {
        $nowZoom=$(this);
        deskZoomIn($nowZoom,1.776);
    });
    $box.on('click','.zoom-in',function () {
        $nowZoom=$(this);
        deskZoomIn($nowZoom,2.5);
    });
    function deskZoomIn(parent,scale) {
        if(nowZoomFlag){
            nowZoomFlag=false;
            var centerY = $html.height()/2-parent.height()/2,
                centerX = $html.width()/2-parent.width()/2,
                x = centerX-$nowZoom[0].getBoundingClientRect().left,
                y = centerY-$nowZoom[0].getBoundingClientRect().top;
            parent.css('transform','translate('+x+'px,'+y+'px) scale('+scale+')')
                .siblings().addClass('zoom-out');
            $other.addClass('zoom-out');
            $tpBg.removeClass('zoom-out');
            $box.addClass('now-zoom');
            if(!$('.is-friend').hasClass('choice')){
                $tpBgDesk.html(parent.index()==0?'主宾':parent.index());
            }
        }
    }

    //点击每个座位
    $box.on('click','.wrapper-big .circle,.zoom-in .circle',function () {
        if(!nowZoomFlag){
            if($('.is-friend').hasClass('choice')){
                return;
            }else if($(this).hasClass('has-man')){
                $tips.finish().fadeIn().delay(1000).fadeOut();
            }else {
                $(this).addClass('has-man').html('<img src="../img/touxiang1.png">');
                $('.is-friend').addClass('choice');
                showPop('#wish-pop');
                $tpBgChair.html($(this).index());
                $nowPeople.html($nowPeople.html()-0+1);
            }
        }
    });

    //点击旁边回去
    $tpBg.on('click',function () {
        $nowZoom.removeAttr('style').siblings().removeClass('zoom-out');
        $other.removeClass('zoom-out');
        $box.removeClass('now-zoom');
        $(this).addClass('zoom-out');
        nowZoomFlag=true;
    });
});